package com.miaowang.salad.activity;

import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.Common;
import com.miaowang.salad.data.MineInfo;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.jobqueue.GetMineInfoJob;
import com.miaowang.salad.jobqueue.JobEvents;
import com.miaowang.salad.jobqueue.NewOrChangeMyCarInfoJob;
import com.miaowang.salad.util.CheckUtil;
import com.miaowang.salad.util.PrefUtils;
import com.path.android.jobqueue.Job;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 我的车辆信息页面 进入页面时，先获取个人信息
 * 
 * @author weihe
 *
 */
public class UserInfoActivity extends BaseActivity {

	private EditText edt_mine_name;
	private EditText edt_mine_phone;
	private EditText edt_rescue_mine_carnum;
	private EditText edt_mine_cartype;
	private EditText edt_mine_carcolor;
	private Button btn_mine_save;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mine_info);
		initComponent();
		initControl();
		getMineInfoFromServer();
	}

	private void initComponent() {
		edt_mine_name = (EditText) findViewById(R.id.edt_mine_name);
		edt_mine_phone = (EditText) findViewById(R.id.edt_mine_phone);
		edt_rescue_mine_carnum = (EditText) findViewById(R.id.edt_rescue_mine_carnum);
		edt_mine_cartype = (EditText) findViewById(R.id.edt_mine_cartype);
		edt_mine_carcolor = (EditText) findViewById(R.id.edt_mine_carcolor);
		btn_mine_save = (Button) findViewById(R.id.btn_mine_save);

	}

	/**
	 * 获取个人信息
	 */
	private void getMineInfoFromServer() {
		if (pd == null) {
			pd = new ProgressDialog(UserInfoActivity.this);
		}
		pd.setTitle("正在获取我的信息...");
		pd.show();
		Job job = new GetMineInfoJob(1);
		CarmapApplication.getInstance().getJobManager().addJobInBackground(job);
	}

	private void initControl() {
		// 保存事件
		btn_mine_save.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String name = edt_mine_name.getText().toString();
				String phone = edt_mine_phone.getText().toString();
				String carnum = edt_rescue_mine_carnum.getText().toString();
				String cartype = edt_mine_cartype.getText().toString();
				String carcolor = edt_mine_carcolor.getText().toString();
				if (CheckUtil.isEmpty(name)) {
					edt_mine_name.requestFocus();
					edt_mine_name.setError("姓名不能为空");
					return;
				}
				if (CheckUtil.isEmpty(phone)) {
					edt_mine_name.requestFocus();
					edt_mine_name.setError("手机不能为空");
					return;
				}
				if (CheckUtil.isEmpty(carnum)) {
					edt_rescue_mine_carnum.requestFocus();
					edt_rescue_mine_carnum.setError("车牌号不能为空");
					return;
				}
				if (CheckUtil.isEmpty(cartype)) {
					edt_mine_cartype.requestFocus();
					edt_mine_cartype.setError("车辆类型不能为空");
					return;
				}
				if (CheckUtil.isEmpty(carcolor)) {
					edt_mine_carcolor.requestFocus();
					edt_mine_carcolor.setError("车辆颜色不能为空");
					return;
				}

				JSONObject json = new JSONObject();
				try {
					json.put("name", name);
					json.put("phone", phone);
					json.put("car_model", cartype);
					json.put("car_license_no", carnum);
					json.put("car_color", carcolor);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if (pd == null) {
					pd = new ProgressDialog(UserInfoActivity.this);
				}
				pd.setTitle("正在上传我的信息...");
				pd.show();
				Job job = new NewOrChangeMyCarInfoJob(json.toString());
				CarmapApplication.getInstance().getJobManager()
						.addJobInBackground(job);
			}
		});
	}

	// EventsBus
	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobSuccess e) {
		if (e.getJobCode() == JobEvents.SEND_MY_INFO) {
			if (pd != null)
				pd.dismiss();
			OkHttpResponseData msg = (OkHttpResponseData) e.getResult();
			if (msg != null) {
				if (msg.getCode() == 0) {
					Toast.makeText(UserInfoActivity.this, msg.getMsg(),
							Toast.LENGTH_SHORT).show();
					if (!CheckUtil.isEmpty(msg.getData())) {
						try {
							JSONObject json = new JSONObject(msg.getData());
							if (json.has("user")) {
								String userString = json.getString("user");
								if (!CheckUtil.isEmpty(userString)) {
									// 将我的信息保存到缓存中
									PrefUtils.putString(UserInfoActivity.this,
											Common.PREF_MY_INFO, userString);
								} else {
									Toast.makeText(UserInfoActivity.this,
											"我的信息修改失败", Toast.LENGTH_SHORT)
											.show();
								}
							} else {
								Toast.makeText(UserInfoActivity.this,
										"我的信息修改失败", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e1) {
							e1.printStackTrace();
						}

					} else {
						Toast.makeText(UserInfoActivity.this, msg.getMsg(),
								Toast.LENGTH_SHORT).show();
					}

				} else {
					Toast.makeText(UserInfoActivity.this, msg.getMsg(),
							Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(UserInfoActivity.this, "网络请求异常",
						Toast.LENGTH_SHORT).show();
			}
		} else if (e.getJobCode() == JobEvents.GET_MY_INFO) {
			if (pd != null)
				pd.dismiss();
			OkHttpResponseData msg = (OkHttpResponseData) e.getResult();
			if (msg != null) {
				if (msg.getCode() == 0) {
					Toast.makeText(UserInfoActivity.this, msg.getMsg(),
							Toast.LENGTH_SHORT).show();
					if (!CheckUtil.isEmpty(msg.getData())) {
						try {
							JSONObject json = new JSONObject(msg.getData());
							if (json.has("user")) {
								String userString = json.getString("user");
								if (!CheckUtil.isEmpty(userString)) {
									MineInfo mineInfo = JSON.parseObject(userString, MineInfo.class);
									edt_mine_name.setText(mineInfo.getName());
									edt_mine_phone.setText(mineInfo.getPhone());
									edt_rescue_mine_carnum.setText(mineInfo.getCar_license_no());
									edt_mine_cartype.setText(mineInfo.getCar_model());
									edt_mine_carcolor.setText(mineInfo.getCar_color());
									// 将我的信息保存到缓存中
									PrefUtils.putString(UserInfoActivity.this,
											Common.PREF_MY_INFO, userString);
								} else {
									Toast.makeText(UserInfoActivity.this,
											"我的信息获取失败", Toast.LENGTH_SHORT)
											.show();
								}
							} else {
								Toast.makeText(UserInfoActivity.this,
										"我的信息获取失败", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e1) {
							e1.printStackTrace();
						}

					} else {
						Toast.makeText(UserInfoActivity.this, msg.getMsg(),
								Toast.LENGTH_SHORT).show();
					}

				} else {
					Toast.makeText(UserInfoActivity.this, msg.getMsg(),
							Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(UserInfoActivity.this, "网络请求异常",
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobFailure e) {
		if (e.getJobCode() == JobEvents.SEND_MY_INFO) {
			if (pd != null)
				pd.dismiss();
		} else if (e.getJobCode() == JobEvents.GET_MY_INFO) {
			if (pd != null)
				pd.dismiss();
		}
	}
}
